package com.itheima.ydd.controller;

import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Cate;
import com.itheima.ydd.service.CateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 分类管理
 */
@Slf4j
@RestController
@RequestMapping("ydd_cate")
public class CateController {
    @Autowired
    private CateService cateService;

    /**
     * 返回分类详情
     *
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(Integer id) {
        log.info("返回分类详情id:{}", id);
        Cate cate = cateService.getById(id);
        return ResultData.ok("", cate);
    }


    /**
     * 分类列表-分页
     *
     * @param catename
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("list")
    public ResultData list(String catename, Integer page, Integer limit) {
        log.info("分类名称catename:{},页码page:{},每页数量limit:{}", catename, page, limit);
        HashMap<String, Object> map = cateService.getPageHashMap(catename, page, limit);
        return ResultData.ok("", map);
    }

    /**
     * 分类删除
     *
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResultData del(Integer id) {
        log.info("分类删除id:{}", id);
        cateService.removeById(id);
        return ResultData.ok("", "分类删除成功");
    }

    /**
     * 分类修改
     *
     * @param cate
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody Cate cate) {
        log.info("分类修改Cate:{}", cate);
        cateService.updateById(cate);
        return ResultData.ok("", "分类修改成功");
    }

    /**
     * 分类添加
     *
     * @param cate
     * @return
     */
    @PostMapping("add")
    public ResultData add(@RequestBody Cate cate) {
        log.info("分类添加Cate:{}", cate);
        cate.setDefault_data(1);
        cateService.save(cate);
        return ResultData.ok("", "分类添加成功");
    }


    /**
     * 分类列表-全部
     *
     * @return
     */
    @GetMapping("all")
    public ResultData all() {
        log.info("分类列表-全部");
        List<Cate> list = cateService.list();
        return ResultData.ok("", list);
    }
}
